def display_menu():
    """显示系统菜单"""
    print("\n===== 学生管理系统 =====")
    print("1. 添加学生信息")
    print("2. 删除学生信息")
    print("3. 修改学生信息")
    print("4. 查询学生信息")
    print("5. 显示所有学生")
    print("6. 退出系统")
    print("=======================")


def add_student(students):
    """添加学生信息"""
    student_id = input("请输入学生学号: ")

    # 检查学号是否已存在
    if student_id in students:
        print("该学号已存在！")
        return

    name = input("请输入学生姓名: ")
    age = input("请输入学生年龄: ")
    gender = input("请输入学生性别: ")
    major = input("请输入学生专业: ")

    # 存储学生信息
    students[student_id] = {
        "姓名": name,
        "年龄": age,
        "性别": gender,
        "专业": major
    }
    print("学生信息添加成功！")


def delete_student(students):
    """删除学生信息"""
    student_id = input("请输入要删除的学生学号: ")

    if student_id in students:
        del students[student_id]
        print("学生信息删除成功！")
    else:
        print("未找到该学生信息！")


def modify_student(students):
    """修改学生信息"""
    student_id = input("请输入要修改的学生学号: ")

    if student_id not in students:
        print("未找到该学生信息！")
        return

    print("请输入新的学生信息（不修改的项直接回车）:")
    name = input("姓名: ")
    age = input("年龄: ")
    gender = input("性别: ")
    major = input("专业: ")

    # 只修改输入了内容的字段
    if name:
        students[student_id]["姓名"] = name
    if age:
        students[student_id]["年龄"] = age
    if gender:
        students[student_id]["性别"] = gender
    if major:
        students[student_id]["专业"] = major

    print("学生信息修改成功！")


def query_student(students):
    """查询学生信息"""
    query_type = input("请选择查询方式（1.按学号 2.按姓名）: ")

    if query_type == "1":
        student_id = input("请输入学生学号: ")
        if student_id in students:
            print(f"\n学号: {student_id}")
            for key, value in students[student_id].items():
                print(f"{key}: {value}")
        else:
            print("未找到该学生信息！")

    elif query_type == "2":
        name = input("请输入学生姓名: ")
        found = False
        for student_id, info in students.items():
            if info["姓名"] == name:
                print(f"\n学号: {student_id}")
                for key, value in info.items():
                    print(f"{key}: {value}")
                found = True
        if not found:
            print("未找到该学生信息！")

    else:
        print("无效的查询方式！")


def display_all(students):
    """显示所有学生信息"""
    if not students:
        print("当前没有学生信息！")
        return

    print("\n===== 所有学生信息 =====")
    for student_id, info in students.items():
        print(f"\n学号: {student_id}")
        for key, value in info.items():
            print(f"{key}: {value}")
    print("=======================")


def main():
    # 用字典存储学生信息，键为学号，值为包含其他信息的字典
    students = {}

    while True:
        display_menu()
        choice = input("请选择操作(1-6): ")

        if choice == "1":
            add_student(students)
        elif choice == "2":
            delete_student(students)
        elif choice == "3":
            modify_student(students)
        elif choice == "4":
            query_student(students)
        elif choice == "5":
            display_all(students)
        elif choice == "6":
            print("感谢使用，再见！")
            break
        else:
            print("无效的选择，请重新输入！")

        input("\n按回车键继续...")


if __name__ == "__main__":
    main()